// src\router\index.js
import { createRouter, createWebHashHistory } from 'vue-router';
const Layout = () => import('@/layout/index.vue');

// 静态路由
// 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
// 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
const constantRoutes = [
  // 重定向
  {
    path: '/redirect',
    name: 'BaseRedirect',
    component: () => import('@/views/admin/base/redirect/index.vue'),
    meta: { hidden: true },
  },
  // 登录页面国际化
  {
    path: '/login',
    name: 'BaseLogin',
    meta: {
      title: 'Login',
      hidden: true,
      key: 'menu.dashboard.login',
    },
    component: () => import('@/views/admin/base/login/index.vue'),
  },
  // 首页
  {
    path: '/',
    name: '/',
    component: Layout,
    redirect: { name: 'Dashboard' },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        meta: {
          title: 'Dashboard',
          icon: 'House',
          key: 'menu.dashboard.title',
          affix: true,
          keepAlive: true,
        },
        component: () => import('@/views/admin/base/dashboard/index.vue'),
      },
      {
        path: '401',
        name: '401',
        meta: { hidden: true },
        component: () => import('@/views/admin/base/error/401.vue'),
      },
      {
        path: '404',
        name: '404',
        meta: { hidden: true },
        component: () => import('@/views/admin/base/error/404.vue'),
      },
      {
        path: '/admin/base/profile',
        name: 'AdminBaseProfile',
        meta: {
          title: 'Profile',
          icon: 'user',
          hidden: true,
          keepAlive: true,
          key: 'menu.dashboard.profile',
        },
        component: () => import('@/views/admin/base/profile/index.vue'),
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
function setupRouter(app) {
  app.use(router);
}

export default router;
export { setupRouter, constantRoutes };
